﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace Sbook.Data
{
    public class DeliveryDetailController:SqlDataProvider
    {
        #region[Delivery_GetByAll]
        public List<Data.DeliveryDetailInfo> DeliveryDetail_GetByAll()
        {
            List<Data.DeliveryDetailInfo> list = new List<Data.DeliveryDetailInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_DeliveryDetail_GetByAll", GetConnection()))
            {
                Data.DeliveryDetailInfo obj = new Data.DeliveryDetailInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.DeliveryDetailIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion

        #region[DeliveyDetail_GetByTop]
        public List<Data.DeliveryDetailInfo> DeliveyDetail_GetByTop(string top, string where, string order)
        {
            List<Data.DeliveryDetailInfo> list = new List<Data.DeliveryDetailInfo>();
            using (SqlCommand cmd = new SqlCommand("sp_DeliveryDetail_GetByTop", GetConnection()))
            {
                Data.DeliveryDetailInfo obj = new DeliveryDetailInfo();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Top", top);
                cmd.Parameters.AddWithValue("@Where", where);
                cmd.Parameters.AddWithValue("@Order", order);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.DeliveryDetailIDataReader(dr));
                    }
                    dr.Close();
                }
            }
            return list;
        }
        #endregion

        #region[DeliveryDetail_Delete]

        public bool DeliveryDetail_Delete(string DeliveryId, string ProductId)
        {
            using (SqlCommand cmd = new SqlCommand("sp_DeliveryDetail_Delete", GetConnection()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@DeliveryId", DeliveryId);
                cmd.Parameters.AddWithValue("@ProductId", ProductId);
                cmd.ExecuteNonQuery();
            }
            return true;
        }

        #endregion

        #region[DeliveryDetail_Insert]
        public bool DeliveryDetail_Insert(Data.DeliveryDetailInfo obj)
        {
            using (SqlCommand cmd = new SqlCommand("sp_DeliveryDetail_Insert", GetConnection()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@DeliveryId", obj.DeliveryId));
                cmd.Parameters.Add(new SqlParameter("@ProductId", obj.ProductId));
                cmd.Parameters.Add(new SqlParameter("@Quantity", obj.Qty));
                cmd.Parameters.Add(new SqlParameter("@Remark", obj.Remark));
                cmd.Parameters.Add(new SqlParameter("@Price", obj.Price));
                cmd.Parameters.Add(new SqlParameter("@Location", obj.Location));

                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region[DeliveryDetail_Update]
        public bool DeliveryDetail_Update(Data.DeliveryDetailInfo obj)
        {
            using (SqlCommand cmd = new SqlCommand("sp_DeliveryDetail_Update", GetConnection()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@DeliveryId", obj.DeliveryId));
                cmd.Parameters.Add(new SqlParameter("@ProductId", obj.ProductId));
                cmd.Parameters.Add(new SqlParameter("@Quantity", obj.Qty));
                cmd.Parameters.Add(new SqlParameter("@Remark", obj.Remark));
                cmd.Parameters.Add(new SqlParameter("@Price", obj.Price));
                cmd.Parameters.Add(new SqlParameter("@Location", obj.Location));

                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion
    }
}